﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class RegisterPanel : MonoBehaviour
{

    public InputField username;
    public InputField nickname;
    public InputField password;
    public Button submitBtn;
    public GameObject reg;
    public GameObject log;

    public GameObject n;

    bool no;
    float showtime;

    void Awake()
    {
        EventDispatcher.RegisterHendler += OnRegisterResponse;
    }
    void OnDestroy()
    {
        EventDispatcher.RegisterHendler -= OnRegisterResponse;

    }

    void Start()
    {

        submitBtn.onClick.AddListener(OnSubmitClick);
    }

    void Update()
    {
        if (no == true)
        {
            showtime += Time.deltaTime;
            if (showtime >= 2)
            {
                showtime = 0;
                n.SetActive(false);
            }
        }
    }

    private void OnSubmitClick()
    {
        Data.ReqRegister req = new Data.ReqRegister();
        req.username = username.text;
        req.nickname = nickname.text;
        req.password = password.text;

        NetworkManager.Instance.Send(Protocal.Register_CMD, ProtoHelper.Serialize(req));
    }

    private void OnRegisterResponse(Data.ReqRegister req)
    {
        if (req.errorCode.code == 1)
        {
            Debug.Log(req.nickname + "注册成功");
            reg.SetActive(false);
            log.SetActive(true);
        }
        else
        {
            Debug.Log(req.errorCode.error);
            n.SetActive(true);
            no = true;
        }
    }

}
